package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.iot.vo.DeviceReaVo;
import com.xiaoshuidi.cloud.module.rooms.mapper.IotDeviceMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @BelongsProject: lease-module-rooms
 * @BelongsPackage: com.xiaoshuidi.cloud.module.rooms.repository
 * @Author: James
 * @CreateTime: 2023-05-26  16:25
 * @Description: TODO
 * @Version: 1.0
 */
@Repository
@RequiredArgsConstructor
public class IotDeviceRepository extends ServiceImpl<IotDeviceMapper, DeviceReaVo> {
    private final IotDeviceMapper iotDeviceMapper;

    public Page<DeviceReaVo> getDeviceList(DeviceReaVo deviceReaVo) {
        Page<DeviceReaVo> page = Page.of(deviceReaVo.getPageNo(), deviceReaVo.getPageSize());
        QueryWrapper<DeviceReaVo> wrapper = Wrappers.query();
//        if (StrUtil.isNotEmpty(deviceReaVo.getWorterType())) {
//            wrapper.eq("der.deviceType", deviceReaVo.getWorterType());
//            if (StringUtils.isNotBlank(deviceReaVo.getRoomName())) {
//                wrapper.and(s -> s
//                        .like(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), "der.roomName", deviceReaVo.getRoomName())
//                        .or().like(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), "der.deviceThirdNo", deviceReaVo.getRoomName()));
//
//            }
//        } else {
//            if (deviceReaVo.getDeviceType().equals("hot_watermeter") || deviceReaVo.getDeviceType().equals("cold_watermeter")) {
//                wrapper.and(s -> s.eq("der.deviceType", "hot_watermeter")
//                                .or().eq("der.deviceType", "cold_watermeter"))
//                        .and(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), s -> s.like("der.roomName", deviceReaVo.getRoomName())
//                                .or().like("der.deviceThirdNo", deviceReaVo.getRoomName()));
//            } else {
//                wrapper.eq("der.deviceType", deviceReaVo.getDeviceType())
//                        .and(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), s -> s.like("der.roomName", deviceReaVo.getRoomName())
//                                .or().like("der.deviceThirdNo", deviceReaVo.getRoomName()))
//                ;
//            }
//        }
        wrapper.eq(StrUtil.isNotEmpty(deviceReaVo.getFloorNo()), "der.floorNo", deviceReaVo.getFloorNo())
                .eq(deviceReaVo.getIsConcentrated() != null, "der.isConcentrated", deviceReaVo.getIsConcentrated())
                .eq(StrUtil.isNotEmpty(deviceReaVo.getBuildingNo()), "der.buildingNo", deviceReaVo.getBuildingNo())
                .eq(StrUtil.isNotEmpty(deviceReaVo.getUnitNo()), "der.unitNo", deviceReaVo.getUnitNo())
                .eq(deviceReaVo.getApartmentId() != null, "der.apartmentId", deviceReaVo.getApartmentId())
//                .and(StrUtil.isNotEmpty(deviceReaVo.getDeviceType()) && "watermeter".equals(deviceReaVo.getDeviceType()),
//                        s -> s.eq("device_type", "hot_watermeter")
//                                .or()
//                                .eq("device_type", "cold_watermeter"))
                .eq(deviceReaVo.getApartmentAreaName() != null, "der.area", deviceReaVo.getApartmentAreaName())
                .in(!CollectionUtils.isEmpty(deviceReaVo.getDeviceNos()), "der.deviceNo", deviceReaVo.getDeviceNos())
                .isNotNull("der.apartmentId");

        wrapper.eq("der.deleted", 0);

        if (StringUtils.isNotBlank(deviceReaVo.getRoomName())) {
            wrapper.and(s -> s
                    .like(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), "der.roomName", deviceReaVo.getRoomName())
                    .or().like(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), "der.deviceThirdNo", deviceReaVo.getRoomName()));

        }
        page = iotDeviceMapper.getDviceList(wrapper, page);
        return page;
    }

    public List<String> getDviceByRoomId(Long roomId) {
        QueryWrapper<DeviceReaVo> wrapper = Wrappers.query();
        wrapper.eq("room_house_id", roomId);
        return iotDeviceMapper.getDviceByRoomId(wrapper);
    }

    public List<DeviceReaVo> getDeviceMapping(Long roomId) {
        QueryWrapper<DeviceReaVo> wrapper = Wrappers.query();
        wrapper.eq("room_house_id", roomId);
        return iotDeviceMapper.getDeviceMapping(wrapper);
    }

    public String getDviceByDeviceType(Long roomId, String deviceType) {
        QueryWrapper<DeviceReaVo> wrapper = Wrappers.query();
        wrapper.eq("room_house_id", roomId);
        wrapper.eq("device_type", deviceType);
        wrapper.eq("deleted", false);
        return iotDeviceMapper.getDviceByDeviceType(wrapper);
    }

    public List<DeviceReaVo> getDviceIds(Long roomId) {
        QueryWrapper<DeviceReaVo> wrapper = Wrappers.query();
        wrapper.eq("room_house_id", roomId);
        wrapper.notIn("device_Type", "doorlock");
        return iotDeviceMapper.getDviceIds(wrapper);
    }

    public List<DeviceReaVo> getDeviceListNoPages(DeviceReaVo deviceReaVo) {
        QueryWrapper<DeviceReaVo> wrapper = Wrappers.query();
        wrapper.eq(StrUtil.isNotEmpty(deviceReaVo.getFloorNo()), "der.floorNo", deviceReaVo.getFloorNo())
                .eq(deviceReaVo.getIsConcentrated() != null, "der.isConcentrated", deviceReaVo.getIsConcentrated())
                .eq(StrUtil.isNotEmpty(deviceReaVo.getBuildingNo()), "der.buildingNo", deviceReaVo.getBuildingNo())
                .eq(StrUtil.isNotEmpty(deviceReaVo.getUnitNo()), "der.unitNo", deviceReaVo.getUnitNo())
                .eq(deviceReaVo.getApartmentId() != null, "der.apartmentId", deviceReaVo.getApartmentId())
                .eq(deviceReaVo.getApartmentAreaName() != null, "der.area", deviceReaVo.getApartmentAreaName())
                .in(!CollectionUtils.isEmpty(deviceReaVo.getDeviceNos()), "der.deviceNo", deviceReaVo.getDeviceNos())
                .isNotNull("der.apartmentId");

        wrapper.eq("der.deleted", 0);

        if (StringUtils.isNotBlank(deviceReaVo.getRoomName())) {
            wrapper.and(s -> s
                    .like(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), "der.roomName", deviceReaVo.getRoomName())
                    .or().like(StrUtil.isNotEmpty(deviceReaVo.getRoomName()), "der.deviceThirdNo", deviceReaVo.getRoomName()));
        }
       return iotDeviceMapper.getDviceListNoPages(wrapper);
    }
}
